<template>
  <div class="legal-document-container">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <i class="el-icon-loading"></i>
      <span>{{ $t('common.loading') }}</span>
    </div>

    <!-- 内容显示 -->
    <template v-else>
      <h1 class="document-title">{{ legalDocumentsContent.title }}</h1>

      <!-- 文档内容 -->
      <div class="document-content" v-html="legalDocumentsContent.content"></div>

      <!-- 空状态 -->
      <div v-if="!legalDocumentsContent.content" class="empty-content">
        <i class="el-icon-document"></i>
        <p>{{ $t('legal.emptyContent') }}</p>
      </div>
    </template>

    <!-- 错误提示 -->
    <el-alert
      v-if="errorMsg"
      :title="errorMsg"
      type="error"
      show-icon
      class="error-alert"
      @close="errorMsg = ''"
    />
  </div>
</template>

<script>
import { getLegalDocumentById } from "@/api/legalDocuments/legalDocuments";

export default {
  name: "LegalDocument",
  data() {
    return {
      loading: true,
      legalDocumentsContent: {},
      errorMsg: ""
    };
  },
  watch: {
    // 监听路由变化重新加载数据
    '$route.params.id': {
      handler(newId) {
        if (newId) {
          this.loadDocument(newId);
        }
      },
      immediate: true
    }
  },
  methods: {
    async loadDocument(id) {
      this.loading = true;
      this.errorMsg = "";

      try {
        const response = await getLegalDocumentById(id);
        this.legalDocumentsContent = response.data;

        // 设置页面标题
        if (response.data.title) {
          document.title = `${response.data.title} - ${this.$t('app.name')}`;
        }
      } catch (error) {
        console.error("获取法律文档失败:", error);
        this.errorMsg = this.$t('legal.loadError');

        // 3秒后自动重试
        setTimeout(() => {
          this.loadDocument(id);
        }, 3000);
      } finally {
        this.loading = false;
      }
    }
  }
};
</script>

<style scoped lang="scss">
.legal-document-container {
  max-width: 1200px;
  margin: 140px auto;
  padding: 20px;
  min-height: 60vh;

  .document-title {
    color: #10162e;
    border-bottom: 2px solid #fe3231;
    padding-bottom: 30px;
    margin-bottom: 30px;
    text-align: center;
    font-weight: 600;
  }

  .document-content {
    line-height: 1.8;
    font-size: 16px;
    color: #333;

    :deep(h2),
    :deep(h3) {
      color: #10162e;
      margin: 25px 0 15px;
      border-left: 4px solid #fe3231;
      padding-left: 10px;
    }

    :deep(p) {
      margin-bottom: 15px;
      text-align: justify;
    }

    :deep(ul) {
      padding-left: 20px;
      margin-bottom: 20px;

      li {
        margin-bottom: 8px;
      }
    }
  }

  .loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 300px;

    .el-icon-loading {
      font-size: 48px;
      color: #fe3231;
      margin-bottom: 20px;
    }
  }

  .empty-content {
    text-align: center;
    padding: 50px 0;
    color: #999;

    .el-icon-document {
      font-size: 60px;
      margin-bottom: 20px;
      display: block;
    }
  }

  .error-alert {
    margin-top: 20px;
  }
}

@media (max-width: 768px) {
  .legal-document-container {
    padding: 15px;

    .document-title {
      font-size: 1.5rem;
    }

    .document-content {
      font-size: 14px;
    }
  }
}
</style>
